package asia.hymsk.spring.controller;

import asia.hymsk.spring.anno.Log;
import asia.hymsk.spring.pojo.Response;
import asia.hymsk.spring.service.UserService;
import org.springframework.web.bind.annotation.*;

/**
 * @author 花于陌上开
 */
@CrossOrigin  //允许跨域访问
@RestController
@RequestMapping("/user/")
public class UserController {
    final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    /**
     * 查询用户名是否存在
     *
     * @param userName 用户名
     * @return 返回状态信息
     */
    @Log
    @GetMapping("/status/exist/")
    public Response userNameIsExist(String userName) {
        return userService.userNameIsExist(userName);
    }

    /**
     * 获取图灵测试验证码
     *
     * @param email 邮箱
     * @return 返回状态信息及图片信息
     */
    @Log
    @PostMapping("/verification/turing/")
    public Response getVerificationTuring(String email) {
        if (email.length() == 0) {
            return Response.error("请先填写邮箱");
        }
        return userService.getVerificationTuring(email);
    }

    /**
     * 获取验证码
     *
     * @param email            邮箱
     * @param verificationCode 图灵测试验证码
     * @return 返回状态信息
     */
    @Log
    @PostMapping("/verification/message/")
    public Response sendVerificationCode(String email, String verificationCode) {
        return userService.sendVerificationCode(email, verificationCode);
    }

    /**
     * 进行注册
     *
     * @param userName         用户名
     * @param email            邮箱
     * @param verificationCode 验证码
     * @param password         密码
     * @return 返回状态信息
     */
    @Log
    @PostMapping("/register/")
    public Response userRegister(String userName, String email, String verificationCode, String password) {
        return userService.userRegister(userName, email, verificationCode, password);
    }

    /**
     * 重置密码
     *
     * @param email            邮箱
     * @param verificationCode 验证码
     * @param password         密码
     * @return 返回状态信息
     */
    @Log
    @PostMapping("/register/reset/")
    public Response userResetPassword(String email, String verificationCode, String password) {
        return userService.userResetPassword(email, verificationCode, password);
    }

    /**
     * 登录
     *
     * @param parameter 用户名/邮箱
     * @param password  密码
     * @return 返回状态信息
     */
    @Log
    @PostMapping("/login/")
    public Response login(String parameter, String password) {
        return userService.login(parameter, password);
    }
}
